#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2022-2-24 15:50
# @Author : huabao.tan
# @File : niquge.py
# @Software: PyCharm

from bs4 import BeautifulSoup
import urllib,re

root = 'http://www.biquge.com/'
urlList = []
chaptername = input('请输入笔趣阁小说章节目录子地址:')
print( u"正在解析章节列表...")

soup = BeautifulSoup(urllib(root+chaptername+u'/').read())

novelname = soup.find('div',id='info').find('h1').get_text()

for result in soup.find(id="list").find("dt").find_next("dt").find_next_siblings("dd"):
        res = result.find_next("a")
        urlList.append(res['href'])

fileHandle = open(novelname+u'.txt','a')

for result in urlList:
        temp = BeautifulSoup(urllib.urlopen(root+result).read())
        #print temp.find(id="content").get_text()
        print (u"正在下载:"+temp.title.text)
        content = temp.find(id="content").get_text().encode('gbk','ignore')
        #rstr = r'<(S*?)[^>]*>.*?|<.*? /> '
        #fileHandle.write(re.sub(rstr, "", content))
        fileHandle.write(content)
fileHandle.close()
print( "~~~~~~~~~~~~~~~~~~~~~~~~~~END~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
#为了避免双击的时候直接一闪退出，在最后面加了这么一句
raw_input("Press <Enter> To Quit!")